import * as BABYLON from '@babylonjs/core/Legacy/legacy'
import urls from './urls'
console.info('游戏框架', BABYLON)

export default class Game {
    _canvas = null //画布
    _engine = null //引擎
    _scene = null //场景
    _camera = null //相机
    _light = null //灯光

    constructor(canvasElement) {
        // Create canvas and engine.
        this._canvas = canvasElement
        console.info('标签', this._canvas)
        this._engine = new BABYLON.Engine(this._canvas, true)
    }

    createScene() {
        // Create a basic BJS Scene object.
        this._scene = new BABYLON.Scene(this._engine)

        // Create a FreeCamera, and set its position to (x:0, y:5, z:-10).
        this._camera = new BABYLON.FreeCamera('camera1', new BABYLON.Vector3(0, 5, -10), this._scene)

        // Target the camera to scene origin.
        this._camera.setTarget(BABYLON.Vector3.Zero())

        // Attach the camera to the canvas.
        this._camera.attachControl(this._canvas, false)

        // Create a basic light, aiming 0,1,0 - meaning, to the sky.
        this._light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), this._scene)
        console.log(BABYLON.SceneLoader)
            // BABYLON.SceneLoader.ImportMesh("Rabbit", urls.srcurl, "Rabbit.babylon", this._scene, function(newMeshes, particleSystems, skeletons) {
            //         let rabbit = newMeshes[1]
            //         rabbit.scaling.scaleInPlace(0.08) //缩小兔子模型

        //     })
        // Create a built-in "sphere" shape with 16 segments and diameter of 2.
        let sphere = BABYLON.MeshBuilder.CreateSphere('sphere1', { segments: 16, diameter: 2 }, this._scene)

        // Move the sphere upward 1/2 of its height.
        sphere.position.y = 1

        // Create a built-in "ground" shape.
        let ground = BABYLON.MeshBuilder.CreateGround('ground1', { width: 6, height: 6, subdivisions: 2 }, this._scene)
    }

    doRender() {
        // Run the render loop.
        this._engine.runRenderLoop(() => {
            this._scene.render()
        })

        // The canvas/window resize event handler.
        window.addEventListener('resize', () => {
            this._engine.resize()
        })
    }
}